﻿Public Class Paises
    Dim tipo As String = ""

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim itm As String
        tipo = "ciudad"
        itm = InputBox("Ingrese nombre de nueva ciudad", "Nueva ciudad")
        If itm <> "" Then addElemento(itm, tipo)
    End Sub
    Sub addElemento(ByVal newItem As String, ByVal newItem1 As String)
        Dim idx As Integer
        If newItem1 = "ciudad" Then
            If Not ComboBox1.Items.Contains(newItem) Then
                idx = ComboBox1.Items.Add(newItem)
            Else
                idx = ComboBox1.Items.IndexOf(newItem)
            End If
            ComboBox1.SelectedIndex = idx
        ElseIf newItem1 = "pais" Then
            If Not ComboBox2.Items.Contains(newItem) Then
                idx = ComboBox2.Items.Add(newItem)
            Else
                idx = ComboBox2.Items.IndexOf(newItem)
            End If
            ComboBox1.SelectedIndex = idx

        End If
    End Sub
    Private Sub ComboBox1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.LostFocus
        Dim newitem As String = ComboBox1.Text
        addElemento(newitem, tipo)
    End Sub
    Private Sub ComboBox2_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.LostFocus
        Dim newitem As String = ComboBox2.Text
        addElemento(newitem, tipo)
    End Sub
    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox2.Items.AddRange(New String() {"Perù", "Bolivia", "Argentina", "Chile", "Ecuador"})
        ComboBox1.Items.AddRange(New String() {"Buenos Aires", "Trujillo", "Lima", "Arequipa", "Santiago", "Quito", "La PAz"})
        ComboBox1.Sorted = True
        ComboBox2.Sorted = True
        ComboBox1.SelectedIndex = 6
        ComboBox2.SelectedIndex = 4

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Visible = False
        Formatos.Visible = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim itm As String
        tipo = "pais"
        itm = InputBox("Ingrese nombre de nuevo Pais", "Nuevo pais")
        If itm <> "" Then addElemento(itm, tipo)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Visible = False
        Fondo.Visible = True
    End Sub
End Class